# vim:set filetype=ruby:
def run(cmd)
  puts cmd
  system cmd
end

def run_spec(args)
  run("spin push #{args}")
  #run("bundle exec rspec --fail-fast #{args}")
end

def spec(file)
  if File.exists?(file)
    run_spec(file)
  else
    puts("Spec: #{file} does not exist.")
  end
end

watch("spec/.*/*_spec\.rb") do |match|
  puts(match[0])
  spec(match[0])
end

watch("lib/(.*/*)\.rb") do |match|
  puts(match[0])
  if File.exists?(p = "spec/"+match[1]+"_spec.rb")
    spec(p)
  else
    spec("spec/fog/external/storage_spec.rb")
  end
end

watch("spec/service_mock\.rb") do |match|
  puts(match[0])
  spec("spec/fog/external/storage_spec.rb")
end